# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=cv2pdb
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.53
pkgrel=1
pkgdesc="Converter of DWARF debug information to PDB files (mingw-w64)"
arch=('any')
url="https://github.com/rainers/cv2pdb"
license=('Artistic2')

options=('!strip')

source=("${_realname}"::"git+https://github.com/rainers/cv2pdb#tag=v${pkgver}")

sha256sums=('SKIP')

die () {
  echo "$*" >&2
  exit 1
}

case "$CARCH" in
i686) WARCH=Win32; OUTDIR=bin/Release;;
x86_64) WARCH=x64; OUTDIR=bin/Release_x64;;
*) die "Unsupported architecture: $CARCH"
esac

prepare () {
  cd "$srcdir/cv2pdb"
  sed -i 's/\(<PlatformToolset>\)[^<]*/\1$(DefaultPlatformToolset)/g' src/cv2pdb.vcxproj
}

build() {
  cd "$srcdir/cv2pdb"
  cmd //c '..\..\..\msbuild\hMSBuild.bat -notamd64 -vsw-version local /p:Configuration=Release /p:Platform='"$WARCH"' src\cv2pdb.vcxproj' ||
  die "MSBuild failed."

  test -z "$SIGNTOOL" ||
  eval "$SIGNTOOL $OUTDIR/cv2pdb.exe"
}

package () {
  cd "$srcdir"/cv2pdb

  install -d "${pkgdir}/${MINGW_PREFIX}/bin"
  install -m755 $OUTDIR/cv2pdb.exe "${pkgdir}/${MINGW_PREFIX}/bin"

  install -d "${pkgdir}/${MINGW_PREFIX}/share/doc/cv2pdb"
  install -m644 CHANGES FEATURES LICENSE README.MD \
    "${pkgdir}/${MINGW_PREFIX}/share/doc/cv2pdb"

  git -C "$srcdir/cv2pdb" clean -dfx
}
